package com.wuyan.locationdog.ui.my

import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.AlarmSave
import com.arcsoft.commonbase.bean.AlarmSetting
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.NotificationUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.databinding.AlarmSetBinding
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MyAlarmSetActivity : BaseBindingActivity<AlarmSetBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()


    override fun initData() {

        setTitleText("报警设置")
        binding.apply {

        }

        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
//        map["deviceId"] = ""
        mViewModel.deviceAlarmSet(map)
    }

    override fun observe() {
        super.observe()

        mViewModel.deviceAlarmSetBean.observe(this) {
            val bean33 = it.content.find { it.alarmType == 33 }//震动报警
            val bean44 = it.content.find { it.alarmType == 44 }//声音  app报警


            binding.apply {
                if (NotificationUtils.areNotificationsEnabled()) {

                    if (bean44?.ifAlarm == true) {
                        appAlarm.setImageResource(R.mipmap.alarm_checked)
                    } else {
                        appAlarm.setImageResource(R.mipmap.alarm_unchecked)
                    }
                    if (bean33?.ifAlarm == true) {
                        vibrateAlarm.setImageResource(R.mipmap.alarm_checked)
                    } else {
                        vibrateAlarm.setImageResource(R.mipmap.alarm_unchecked)
                    }

                } else {
                    appAlarm.setImageResource(R.mipmap.alarm_checked_gray)
                    vibrateAlarm.setImageResource(R.mipmap.alarm_checked_gray)
                }
                appAlarm.setOnClickListener {
                    if (NotificationUtils.areNotificationsEnabled()) {


                        bean44?.let {
                            bean44!!.ifAlarm = !bean44.ifAlarm
                            if (bean44?.ifAlarm == true) {
                                appAlarm.setImageResource(R.mipmap.alarm_checked)
                            } else {
                                appAlarm.setImageResource(R.mipmap.alarm_unchecked)
                            }
                            val list = (mutableListOf<AlarmSetting>())
                            list.add(AlarmSetting(it.alarmChannel.map { it.code }, it.alarmType, it.ifAlarm, it.paramValue))
                            bean33?.let {
                                list.add(AlarmSetting(it.alarmChannel.map { it.code }, it.alarmType, it.ifAlarm, it.paramValue))
                            }
                            list.add(AlarmSetting(it.alarmChannel.map { it.code }, it.alarmType, it.ifAlarm, it.paramValue))
                            val alarmSave = AlarmSave(list)
                            mViewModel.deviceAlarmSave(alarmSave)
                        }
                    }
                }
                vibrateAlarm.setOnClickListener {
                    if (NotificationUtils.areNotificationsEnabled()) {


                        bean33?.let {
                            bean33!!.ifAlarm = !bean33.ifAlarm
                            if (bean33?.ifAlarm == true) {
                                vibrateAlarm.setImageResource(R.mipmap.alarm_checked)
                            } else {
                                vibrateAlarm.setImageResource(R.mipmap.alarm_unchecked)
                            }
                            val list = (mutableListOf<AlarmSetting>())
                            list.add(AlarmSetting(it.alarmChannel.map { it.code }, it.alarmType, it.ifAlarm, it.paramValue))
                            bean44?.let {
                                list.add(AlarmSetting(it.alarmChannel.map { it.code }, it.alarmType, it.ifAlarm, it.paramValue))
                            }
                            val alarmSave = AlarmSave(list)
                            mViewModel.deviceAlarmSave(alarmSave)
                        }
                    }
                }
                ifAlarm.setOnCheckedChangeListener { button, checked ->
                    AppUtils.launchAppDetailsSettings()
                    if (checked) {
                        appAlarm.setImageResource(R.mipmap.alarm_checked)
                        vibrateAlarm.setImageResource(R.mipmap.alarm_checked)
                    } else {
                        appAlarm.setImageResource(R.mipmap.alarm_checked_gray)
                        vibrateAlarm.setImageResource(R.mipmap.alarm_checked_gray)
                    }

                }
            }

        }

    }

    override fun onResume() {
        super.onResume()
        if (NotificationUtils.areNotificationsEnabled()) {
            binding.ifAlarm.setChecked(true)
        } else {
            binding.ifAlarm.setChecked(false)
        }
    }

}